package com.chen.controller;

import com.chen.entity.TestEntity;
import com.chen.entity.Users;
import com.chen.controller.service.TestEntityService;
import com.chen.controller.service.TtService;
import org.apache.poi.ss.usermodel.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Title:
 * @Description: to do
 * @Author: chenjiwei
 * @Date: 2020-10-28 22:50
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/testApi")
public class TestController {
    private Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestEntityService testEntityService;

    @Autowired
    private TtService ttService;

    @RequestMapping(value="/findTest", method = RequestMethod.GET)
    public List<TestEntity> test(){
        List<TestEntity> t = testEntityService.findTest();
        return t;
    }

    // 分页
    @RequestMapping(value="/findTestByPage", method = RequestMethod.GET)
    public Page<TestEntity> findTestByPage(){
        return testEntityService.findTestByPage(null);
    }

    // Headers设置为：Content-Type:application/json
    // 请求body设置为：{"id":"1","text":"cjw"}
    @RequestMapping(value = "/requestBodyTest", method = RequestMethod.POST)
    public TestEntity requestBodyTest(@RequestBody TestEntity t){
        info(t);
        return t;
    }

    private void info(@RequestBody TestEntity t) {
        log.info(t.toString());
    }

    // 根据sql语句进行请求 调用entityManage 对数据库进行操作
    @RequestMapping(value = "/findBySql", method = RequestMethod.GET)
    public List<Map<String, Object>> findBySql(){
        System.out.println("8898588");
        return testEntityService.findSql();
    }

    @RequestMapping(value="/exp",method = RequestMethod.GET)
    public Byte[] exp(){
        return null;
    }

    @RequestMapping(value="/imp", method = RequestMethod.GET)
    public void imp(@RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        System.out.println("uploadFile");
        Workbook workbook = WorkbookFactory.create(uploadFile.getInputStream());
        Sheet sheetAt = workbook.getSheetAt(0);
        List<Object[]> list = new ArrayList<>();
        List<Users> userList = new ArrayList<>();
        for(int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++){
            if(i == 0) continue; // 过滤表头
            Row row = sheetAt.getRow(i);
            Object[] objects = new Object[row.getPhysicalNumberOfCells()];
            int index = 0;
            for(Cell cell : row){
                if (cell.getCellType().equals(CellType.NUMERIC)) {
                    objects[index] = (int) cell.getNumericCellValue();
                }
                if (cell.getCellType().equals(CellType.STRING)) {
                    objects[index] = cell.getStringCellValue();
                }
                if (cell.getCellType().equals(CellType.BOOLEAN)) {
                    objects[index] = cell.getBooleanCellValue();
                }
                if (cell.getCellType().equals(CellType.ERROR)) {
                    objects[index] = cell.getErrorCellValue();
                }
                index++;
            }
            list.add(objects);


        }
        for(int i = 0; i < list.size(); i++){
            Object[] os = list.get(i);
            for(int j = 0; j < os.length; j++){
                System.out.print(os[j]+" ");
            }
            System.out.println();
        }
    }
    @RequestMapping(value = "/imp2",method = RequestMethod.GET)
    public void imp2(@RequestParam("fileUpload") MultipartFile fileUpload) throws IOException, ParseException {
        System.out.println("fileUpload");
        Workbook sheets = WorkbookFactory.create(fileUpload.getInputStream());
        ArrayList<Data> datas = new ArrayList<>();
//        获取第一个工作空间
        Sheet sheetAt = sheets.getSheetAt(0);
        int physicalNumberOfRows = sheetAt.getPhysicalNumberOfRows();
        for(int i = 0; i < physicalNumberOfRows; i++){
            if(i == 0) continue;
            Row row = sheetAt.getRow(i);
            Data d = new Data();
//            当id为纯数字时，导出字符串数据报错
            if(row.getCell(0).getCellType().equals(CellType.STRING))
                d.setId(row.getCell(0).getStringCellValue());
            else
                System.out.println("导出id异常");
            if(row.getCell(1).getCellType().equals(CellType.STRING))
                d.setName(row.getCell(1).getStringCellValue());
            else
                System.out.println("导出名字异常");
            if(row.getCell(2).getCellType().equals(CellType.NUMERIC))
                d.setAge((int) row.getCell(2).getNumericCellValue());
            else
                System.out.println("导出年龄异常");
            Cell cell3 = row.getCell(3);
            if(cell3.getCellType().equals(CellType.NUMERIC))
                d.setMoney(BigDecimal.valueOf(cell3.getNumericCellValue()));
            else System.out.println("导出金额异常");

            Cell cell4 = row.getCell(4);
            d.setDataDt(cell4.getDateCellValue());

            datas.add(d);
        }

        datas.forEach(d -> System.out.println(d.toString()));

    }

    @RequestMapping(value = "/tt", method = RequestMethod.GET)
    public void todoTt(){
        List list = ttService.counts();
        Object[] o = (Object[]) list.get(0);
        // 数据库中的字段使用的类型是bigint
        // 转成Integer就会报如下错
        // java.math.BigInteger cannot be cast to java.lang.Integer
        // 的解决方式如下 转成String 再parseInt
        System.out.println(Integer.parseInt(o[0].toString()));
        System.out.println(Integer.parseInt(o[1].toString()));
        System.out.println(Integer.parseInt(o[2].toString()));
    }
}


class Data{
    private String id;
    private String name;
    private int age;
    private BigDecimal money;
    private Date dataDt;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public BigDecimal getMoney() {
        return money;
    }

    public Date getDataDt() {
        return dataDt;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    public void setDataDt(Date dataDt) {
        this.dataDt = dataDt;
    }

    @Override
    public String toString() {
        return "Data{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", money=" + money +
                ", dataDt=" + dataDt +
                '}';
    }
}
